<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">分布式聊天系统</h3>
      <el-form-item prop="chatRoomId">
        选择房间号:<br/>
          <el-select clearable v-model="registerForm.chatRoomId" @change="selectChange" style="width: 350px">
            <el-option v-for="(item,index) of chatRooms" :key="index" :label="item.value" :value="item.key">{{item.value}}</el-option>
          </el-select>

      </el-form-item>

      <el-form-item prop="nickName">
        输入昵称：
        <el-input v-model="registerForm.nickName" type="text" auto-complete="off" placeholder="请输入昵称">
        </el-input>
      </el-form-item>
       <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">立即进入聊天</span>
         </el-button>

      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2024 smart.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      chatRoomId:'CHAT_ROOM_N_01',
      chatRooms: [
        {
          key: 'CHAT_ROOM_N_01',
          value: '一号房间'
        },
        {
          key: 'CHAT_ROOM_N_02',
          value: '二号房间'
        },
        {
          key: 'CHAT_ROOM_N_03',
          value: '三号房间'
        },
        ],

      registerForm: {
        nickName: "",
        chatRoomId: "",

      },
      registerRules: {
        nickName: [
          { required: true, trigger: "blur", message: "请输入您的昵称" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        chatRoomId: [
          { required: true, trigger: "blur", message: "请选择房间号" },
         ]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {

  },
  methods: {
    selectChange(val  ){
      this.chatRoomId = val ;
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const nickName = this.registerForm.nickName;
          const chatRoomId = this.chatRoomId ;
          this.$alert("<font color='red'> 恭喜您，该系统允许使用 【" + nickName + "】作为昵称 赶快畅聊吧！</font>", '系统提示', {
            dangerouslyUseHTMLString: true,
            type: 'success'
          }).then(() => {
            this.loading = false;
            this.$router.push(
              {
              path : '/chat',
              query: {
              chatRoomId: chatRoomId, nickName: nickName
              }
              }
              );

          }).catch(() => {});
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height:800px;
  width: 98%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
